package com.example.dc.Controller;

import com.example.dc.Service.PersonalSerive;
import com.example.dc.Service.QuestionService;
import com.example.dc.domain.Question;
import com.example.dc.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/question")
public class QuestionControll {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private PersonalSerive personalSerive;

    @GetMapping("/random")
    public Result getRandomQuestion(Integer userId) {
        System.out.println(userId);
        Question randomQuestion = questionService.getRandomQuestion(userId);

        System.out.println("抽到的问题："+randomQuestion.getContent());
        return Result.success(randomQuestion);
    }

    @GetMapping("/chatgpt")
    public Result chatgpt(Integer userId,String question){
        //此处补充
        return Result.success();
    }

    @PostMapping("/anwser")
    public Result getAnwser(@RequestBody Map<String, Object> requestData) {
        System.out.println("YES1");
        Integer personalQuestionId=(Integer) requestData.get("personalQuestionId");
        Integer questionId = (Integer) requestData.get("questionId");
        String question = (String) requestData.get("question");
        String anwser = (String) requestData.get("anwser");
        Integer userId = (Integer) requestData.get("userId");
        //先插入用户回答记录
        Map<String,Object> result=questionService.matchAnwserKeyword(personalQuestionId,questionId,question,anwser,userId);
        System.out.println("YES2");
        //返回分数
        Double score=0.0;
        if(result.get("score")!=null){
            score=(Double) result.get("score");
            //乘以倍数
            //先查询该问题倍率
            Double mutiple= personalSerive.getMutiple(questionId,userId);
            score*=mutiple;
            result.put("score",score);
        }

        return Result.success(result);
    }

}
